<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org"
	xmlns:winter="http://www.winter.org"
	xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
	layout:decorator="/layouts/layout">
<head>
	<title>Templates</title>
</head>
<body>
	<section layout:fragment="content">
		<winter:output id="TemplatesOutput" class="height-100">
			<form id="TemplatesForm" th:object="${templates}" class="form">	
				<div th:if="${templates.context.templates}" >
					<winter:output id="TemplateOutput">
						<div id="TemplateDialogContainer" th:if="${templates.context.nuevo or templates.context.editar}">
							<winter:dialog id="TemplatesDialog" width="80%" height="auto" title="${templates.titulo}"  
								contentClass="lista-atributos" dialogClass="activar-lista-dialog">
								<div id="ListaContainer" class="form-container">
				    				<winter:row>
										<winter:inputtext
											id="NuevoTemplateNombre" 
											field="*{template.nombre}" 
											label="#{label.template.nombre}"
											labeldisplay="col-xs-4"
											inputdisplay="col-xs-8"
											containerdisplay="row padded"
											class="winter-input-text"
											type="char"
											required="true"
											requiredimage="/images/icons/exclamation-small-red.png"
											requiredmessage="#{label.template.nombre.requerido}">
										</winter:inputtext>
										<winter:select id="selectTipoTemplate" field="*{template.tipoTemplate}"
											collection="tip, rowtip : ${templates.tiposTemplate}"
											value="@{${tip.id}+'|'+${tip.descripcion}}" text="@{${tip.descripcion}}"
											label="#{label.template.tipo}" 
											required="true" modern="true"
											showglobalmessage="true"
											requiredmessage="#{label.template.tipo.requerido}">
										</winter:select>
										<winter:select id="selectTipoObjeto" field="*{template.tipoObjeto}"
											collection="obj, rowobj : ${templates.tiposObjeto}"
											value="@{${obj.id}+'|'+${obj.descripcion}}" text="@{${obj.descripcion}}"
											label="#{label.template.objeto}" 
											required="true" modern="true"
											showglobalmessage="true"
											requiredmessage="#{label.template.objeto.requerido}">
										</winter:select>
									</winter:row>
									<winter:row>
										<winter:output id="TextEditorOutput">
											<winter:text-editor
												id="TextoTemplate"
												label="#{label.template.texto}"
												field="*{template.texto}"
												labeldisplay="col-xs-8">
					                        </winter:text-editor>
										</winter:output>
									</winter:row>
								</div>
								<winter:row class="button-panel-right" padded="true">
									<winter:ajaxSubmit 
										id="CrearTemplate" 
										form="TemplatesForm" 
										url="@{'/templates/crear'}" 
										update="TemplatesOutput, ListasTablaTemplatesOutput"
										object="templates"
										value="#{label.operaciones.guardar}"
										showstatus="true"
										class="btn btn-cyan"
										if="${templates.context.nuevo}"
										process="ListaContainer"
										oncomplete="@{'Winter.ui.CloseDialog(\'TemplatesDialog\')'}">
									</winter:ajaxSubmit>
									<winter:ajaxSubmit 
										id="ActualizarLista" 
										form="TemplatesForm" 
										url="@{'/templates/actualizar'}" 
										update="TemplatesOutput, ListasTablaTemplatesOutput"
										object="templates"
										value="#{label.operaciones.guardar}"
										showstatus="true"
										class="btn btn-cyan"
										if="${templates.context.editar}"
										process="ListaContainer"
										oncomplete="@{'Winter.ui.CloseDialog(\'TemplatesDialog\')'}">
									</winter:ajaxSubmit>
									<winter:ajaxSubmit 
										id="CrearTemplateCancelar" 
										form="TemplatesForm" 
										url="@{'/templates/cancelar'}"
										update="TemplatesOutput"
										object="templates"
										value="#{label.operaciones.cancelar}"
										showstatus="true"
										class="btn btn-danger"
										immediate="true"
										oncomplete="@{'Winter.ui.CloseDialog(\'TemplatesDialog\')'}">
									</winter:ajaxSubmit>
								</winter:row>
							</winter:dialog>
		  				</div>		
					</winter:output>
				</div>
								
				<winter:output id="ContextMenuOutput">
					<winter:context-menu id="ContextMenu" selector="@{'.Editor-container'}" class="dropdown-menu dropdown-context-menu">
						<winter:context-menu-item id="@{${rowStatVar.index}+'variable'}"
							name="${var.texto}" icon="@{'fa-plus icon'}" type="{'fa-plus icon'}" 
							url="@{'/templates/selectVariable/'+${var.id}}" 
							dynamic="true"
							each="var, rowStatVar : ${templates.variables}"
							update="TextEditorOutput, ContextMenuOutput" form="TemplatesForm" object="templates"
							process="ListaContainer">
						</winter:context-menu-item>
					</winter:context-menu>
				</winter:output>
				
				<div th:if="${templates.context.templates}">
					<div id="BarraTemplates" class="headerbar">
						<div class="form-title" th:text="@{'Templates'}">Templates</div>
					</div>
					<winter:row padded="true">
						<winter:ajaxsubmit 
							id="@{'NuevoTemplate'}"
							form="TemplatesForm"
							url="@{'/templates/nuevoTemplate'}" 
							update="TemplatesOutput, TemplateOutput, ContextMenuOutput"
							value="Agregar Template"
							object="templates"
							effect="fade"
							showstatus="true"
							class="btn btn-cyan"
							oncomplete="@{'Winter.ui.OpenDialog(\'TemplatesDialog\')'}"
							immediate="true">
						</winter:ajaxsubmit>
					</winter:row>
					<winter:output id="ListasTablaTemplatesOutput">
						<winter:datatable 
							id="TemplatesTabla"
							customLanguage="true"
							paginate="true"
							jQuery="true"
							retrieve="true"
							emptyTableText="No se encontraron Datos!"
							infoText="Mostrando _START_ al _END_ de _TOTAL_ registros"
							emptyTableInfoText="Mostrando 0 al 0 de 0 registros"
							searchText="Buscar"
							lengthMenu="Mostrar _MENU_ registros"
							if="${!#lists.isEmpty(templates.templates)}">
							<thead>
								<tr>
									<th th:text="@{'id'}"></th>
									<th th:text="@{'Tipo'}"></th>
									<th th:text="@{'Nombre'}"></th>
									<th th:text="@{'Texto'}"></th>
									<th th:text="@{'Operaciones'}"></th>
								</tr>
							</thead>
							<tbody>
								<tr th:each="listaItem, rowStat : ${templates.templates}">
									<td th:text="${listaItem.id}"></td>
									<td th:text="${listaItem.tipoTemplate.descripcion}"></td>
									<td th:text="${listaItem.nombre}"></td>
									<td th:text="${#strings.abbreviate(listaItem.texto,100)}" th:title="${listaItem.texto}"></td>
									<td class="tabla-operaciones">
										<div class="button-toolbar">										
											<winter:ajaxlink 
												id="@{${rowStat.index}+'ModificarTemplate'}" 
												form="TemplatesForm"
												url="@{'/templates/editar/'+${rowStat.index}}" 
												update="TemplatesOutput, TemplateOutput, ContextMenuOutput"
												title="Editar"
												object="templates"
												immediate="true"
												tip="true"
												class="ajax-submit icon-edit-sign icon-2x"
												oncomplete="@{'Winter.ui.OpenDialog(\'TemplatesDialog\')'}"
												showstatus="true">
											</winter:ajaxlink>
											<winter:ajaxlink 
												id="@{${rowStat.index}+'EliminarTemplate'}" 
												form="TemplatesForm"
												url="@{'/templates/eliminar/'+${rowStat.index}}" 
												update="TemplatesOutput, ListasTablaTemplatesOutput"
												title="Eliminar"
												tip="true"
												object="templates"
												effect="fade"
												showstatus="true"
												immediate="true"
												confirmationrequired="true"
												confirmationtext="Está seguro que desea realizar la operación?"													
												class="ajax-submit icon-remove-sign icon-2x">
											</winter:ajaxlink>																									
										</div>
									</td>
								</tr>
							</tbody>
						</winter:datatable>
					</winter:output>
				</div>
			</form>
		</winter:output>
	</section>
</body>
</html>